package hasthi.metamodel.utils;
import java.util.Date;

import hasthi.actuators.*;

public class SystemHealthState {
    public static String SYSTEM_HEALTH_STATE_KEY = "SystemHealthState";
    private boolean systemHealthy = true;
    private long systemFailedTime = -1;
    private boolean failureNotified = false;
    private boolean recoveryTriggeredAfterFailure = false;
    
    private long RFTFailedTime = -1;
    private long WANMountFailedTime = -1;
    
    private ActionCenter system;
    
    public SystemHealthState(ActionCenter system){
        this.system = system;
        system.put(SYSTEM_HEALTH_STATE_KEY, this);
    }
    
    public void setSystemFailed(){
        if(systemHealthy){
            this.systemHealthy = false;
            this.systemFailedTime = System.currentTimeMillis();
            failureNotified = false;
            recoveryTriggeredAfterFailure = false;
            system.getActionContext().getBaseContext().getDlogger().info("Failure In the managed system detected at "+ new Date(systemFailedTime));
        }
    }
    
    public void setSystemHealthy(){
        this.systemHealthy = true;
        this.systemFailedTime = -1;
        failureNotified = false;
        system.getActionContext().getBaseContext().getDlogger().info("Managed system recovered at "+ new Date());
    }
    
    public boolean isSystemHealthy() {
        return systemHealthy;
    }
    public void setSystemHealthy(boolean isSystemHealthy) {
        this.systemHealthy = isSystemHealthy;
    }
    public long getSystemFailedTime() {
        return systemFailedTime;
    }
    public void setSystemFailedTime(long systemFailedTime) {
        this.systemFailedTime = systemFailedTime;
    }
    public boolean isFailureNotified() {
        return failureNotified;
    }
    public void setFailureNotified(boolean failureNotified) {
        this.failureNotified = failureNotified;
    }

    public boolean isRecoveryTriggeredAfterFailure() {
        return recoveryTriggeredAfterFailure;
    }

    public void setRecoveryTriggeredAfterFailure(boolean recoveryTriggeredAfterFailure) {
        this.recoveryTriggeredAfterFailure = recoveryTriggeredAfterFailure;
    }

    public long getRFTFailedTime() {
        return RFTFailedTime;
    }

    public void setRFTFailed() {
        if(RFTFailedTime == -1){
            RFTFailedTime = System.currentTimeMillis();    
        }
    }

    public long getWANMountFailedTime() {
        return WANMountFailedTime;
    }

    public void setWANMountFailed() {
        if(WANMountFailedTime == -1){
            WANMountFailedTime = System.currentTimeMillis();    
        }
    }
    
    public void setRFTRecovered() {
        RFTFailedTime = -1;
    }
    
    public void setWANMountRecovered() {
        WANMountFailedTime = -1;
    }
    
    public boolean isRFTHealthy() {
        return RFTFailedTime == -1;
    }
    
    public boolean isWANMountHealthy() {
        return WANMountFailedTime == -1;
    }
    
}
